package fr.umlv.m2.recipes.ideas.features;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import fr.umlv.m2.recipes.ideas.bean.IngredientBean;
import fr.umlv.m2.recipes.ideas.bean.KeywordBean;
import fr.umlv.m2.recipes.ideas.bean.RecipeBean;
import fr.umlv.m2.recipes.ideas.bean.RecipeToIngredientBean;
import fr.umlv.m2.recipes.ideas.bean.RecipeToKeywordBean;
import fr.umlv.m2.recipes.ideas.bean.StepBean;

/**
 * Define a tool to handle the recipe parsing
 * Use to make a link between the Beans 
 * @author Christophe Chillan et Younes Chandarli
 *
 */
public class RecipeGroupFeatures {
	private final List<RecipeBean> recipes;
	private final List<IngredientBean> ingredients;
	private final List<KeywordBean> keywords;
	private final Map<String, Integer> ingredientFields;
	private final Map<String, Integer> keywordFields;
	private final List<StepBean> steps;
	private final List<RecipeToKeywordBean> recipesToKeywords;
	private final List<RecipeToIngredientBean> recipesToIngredients;
	
	/**
	 * Define an element use to parse the XML file
	 */
	public RecipeGroupFeatures(){
		recipes = new ArrayList<RecipeBean>();
		ingredients = new ArrayList<IngredientBean>();
		keywords = new ArrayList<KeywordBean>();
		ingredientFields = new HashMap<String, Integer>();
		keywordFields = new HashMap<String, Integer>();
		steps = new ArrayList<StepBean>();
		recipesToIngredients = new LinkedList<RecipeToIngredientBean>();
		recipesToKeywords = new LinkedList<RecipeToKeywordBean>();
	}

	/**
	 * Get All the {@link RecipeBean} of the XML file
	 * @return every recipe listed in the XML file
	 */
	public List<RecipeBean> getRecipes() {
		return Collections.unmodifiableList(recipes);
	}
	
	/**
	 * Add a RecipeBean in the Recipe List
	 * @param recipe
	 * @return true if the recipe has been added, false otherwise
	 */
	public boolean addRecipeBean(RecipeBean recipe){
		return recipes.add(recipe);
	}
	
	/**
	 * Add a RecipeBean in the Recipe List at the specified index
	 * @param index
	 * @param recipe
	 */
	public void addRecipeBean(int index, RecipeBean recipe){
		recipes.add(index, recipe);
	}

	/**
	 * Get All the {@link IngredientBean} of the XML file
	 * @return every ingredient in the XML file
	 */
	public List<IngredientBean> getIngredients() {
		return Collections.unmodifiableList(ingredients);
	}
	
	/**
	 * Get All the {@link IngredientBean} fields with their related id
	 * @return a {@link Map} of the {@link IngredientBean} fields
	 */
	public Map<String, Integer> getIngredientFields(){
		return Collections.unmodifiableMap(ingredientFields);
	}
	
	/**
	 * Add an ingredient if it doesn't exists and replace it otherwise
	 * @param ingredient
	 * @param id
	 */
	public void putIngredientBean(IngredientBean ingredient,int id){
		if(!ingredientFields.containsKey(ingredient.getName())){
			ingredients.add((id-1), ingredient);
			ingredientFields.put(ingredient.getName(), id);
		}
	}


	/**
	 * Get All the {@link KeywordBean} of the XML file
	 * @return every keyword in the XML file
	 */
	public List<KeywordBean> getKeywords() {
		return Collections.unmodifiableList(keywords);
	}
	
	
	/**
	 * Add a keyword if it doesn't exists and replace it otherwise
	 * @param keyword
	 * @param id
	 */
	public void putKeywordBean(KeywordBean keyword, int id){
		if(!keywordFields.containsKey(keyword.getKeyword())){
			keywords.add((id-1), keyword);
			keywordFields.put(keyword.getKeyword(), id);
		}
	}
	/**
	 * Get All the {@link KeywordBean} Fields with their related id
	 * @return a {@link Map} of the Keyword Fields
	 */
	public Map<String, Integer> getKeywordFields(){
		return Collections.unmodifiableMap(keywordFields);
	}

	/**
	 * Get All the {@link StepBean} of the XML file
	 * @return every step in the XML file
	 */
	public List<StepBean> getSteps() {
		return Collections.unmodifiableList(steps);
	}

	/**
	 * Add a StepBean in the Step List
	 * @param step
	 * @return true if the step has been added, false otherwise
	 */
	public boolean addStepBean(StepBean step){
		return steps.add(step);
	}
	
	/**
	 * Get All the liaisons between a recipe and a keyword 
	 * @return All the liaison in the XML file
	 */
	public List<RecipeToKeywordBean> getRecipesToKeywords() {
		return Collections.unmodifiableList(recipesToKeywords);
	}

	/**
	 * Add a liaison between and recipe a keyword
	 * @param recipeToKeyword
	 * @return true if the liaison has been added, false otherwise
	 */
	public boolean addRecipeToKeyword(RecipeToKeywordBean recipeToKeyword){
		return recipesToKeywords.add(recipeToKeyword);
	}

	/**
	 * Get All the liaisons between a recipe and an ingredient 
	 * @return All the liaison in the XML file
	 */
	public List<RecipeToIngredientBean> getRecipesToIngredients() {
		return Collections.unmodifiableList(recipesToIngredients);
	}
	
	/**
	 * Add a liaison between and recipe an ingredient
	 * @param recipeToIngredient
	 * @return true if the liaison has been added, false otherwise
	 */
	public boolean addRecipeToIngredient(RecipeToIngredientBean recipeToIngredient){
		return recipesToIngredients.add(recipeToIngredient);
	}

	
	
}
